﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Xml;
public partial class WebSite_t_paper_edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserId"] == null || Session["UserIdentity"].ToString() != "1")
        {
            Response.Redirect("Default.aspx", false);
            HttpContext.Current.ApplicationInstance.CompleteRequest();
            return;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            string UserId = Session["UserId"].ToString();
            DataSet ds = new DataSet("questionnaire");
            ds.Tables.Add("question");
            ds.Tables["question"].Columns.Add("主题", Type.GetType("System.String"));
            ds.Tables["question"].Columns.Add("Id", Type.GetType("System.String"));
            ds.Tables["question"].Columns.Add("标题", Type.GetType("System.String"));
            ds.Tables["question"].Columns.Add("选项一", Type.GetType("System.String"));
            ds.Tables["question"].Columns.Add("选项二", Type.GetType("System.String"));
            ds.Tables["question"].Columns.Add("选项三", Type.GetType("System.String"));
            ds.Tables["question"].Columns.Add("选项四", Type.GetType("System.String"));
            DataRow newRow_1;
            newRow_1 = ds.Tables["question"].NewRow();
            newRow_1["主题"] = this.TextBox1.Text.ToString();
            newRow_1["标题"] = this.TextBox2.Text.ToString();
            newRow_1["选项一"] = this.TextBox3.Text.ToString();
            newRow_1["选项二"] = this.TextBox4.Text.ToString();
            newRow_1["选项三"] = this.TextBox5.Text.ToString();
            newRow_1["选项四"] = this.TextBox6.Text.ToString();



            string sFileName = System.Web.HttpContext.Current.Server.MapPath("~/");
            string filename = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString() + "-" + DateTime.Now.Hour.ToString() + "-" + DateTime.Now.Minute.ToString() + "-" + DateTime.Now.Second.ToString() + "-" + UserId;
            sFileName = sFileName + "xml\\" + filename + ".xml";
            QuestionnaireHandler Qhandler = new QuestionnaireHandler();
            int QuestionId;
            QuestionId = Qhandler.AddQuestionnaire(this.TextBox1.Text.ToString(), DateTime.Now, UserId, sFileName);
            newRow_1["Id"] = QuestionId.ToString();
            ds.Tables["question"].Rows.Add(newRow_1);
            string sXml = ds.GetXml();



            StreamWriter sr = File.CreateText(sFileName);
            sr.WriteLine(sXml);
            sr.Close();
            Response.Redirect("t_paper_check.aspx?id=" + QuestionId, false);
            return;
        }
        catch(Exception ex)
        {
            //跳转到错误处理页面。
            Session["ErrorMessage"] = ex.Message;
            Session["AtPage"] = "t_paper_edit.aspx";
            Response.Redirect("t_error.aspx", false);
            HttpContext.Current.ApplicationInstance.CompleteRequest();
            return;
        }

    }
}
